package cn.iocoder.yudao.module.cmt.controller.admin.team;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.team.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.team.TeamMemberDO;
import cn.iocoder.yudao.module.cmt.service.team.TeamMemberService;

@Tag(name = "管理后台 - 团队成员")
@RestController
@RequestMapping("/cmt/team-member")
@Validated
public class TeamMemberController {

    @Resource
    private TeamMemberService teamMemberService;

    @PostMapping("/create")
    @Operation(summary = "创建团队成员")
    @PreAuthorize("@ss.hasPermission('cmt:team-member:create')")
    public CommonResult<Long> createTeamMember(@Valid @RequestBody TeamMemberSaveReqVO createReqVO) {
        return success(teamMemberService.createTeamMember(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新团队成员")
    @PreAuthorize("@ss.hasPermission('cmt:team-member:update')")
    public CommonResult<Boolean> updateTeamMember(@Valid @RequestBody TeamMemberSaveReqVO updateReqVO) {
        teamMemberService.updateTeamMember(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除团队成员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:team-member:delete')")
    public CommonResult<Boolean> deleteTeamMember(@RequestParam("id") Long id) {
        teamMemberService.deleteTeamMember(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除团队成员")
    @PreAuthorize("@ss.hasPermission('cmt:team-member:delete')")
    public CommonResult<Boolean> deleteTeamMemberList(@RequestParam("ids") List<Long> ids) {
        teamMemberService.deleteTeamMemberListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得团队成员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:team-member:query')")
    public CommonResult<TeamMemberRespVO> getTeamMember(@RequestParam("id") Long id) {
        TeamMemberDO teamMember = teamMemberService.getTeamMember(id);
        return success(BeanUtils.toBean(teamMember, TeamMemberRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得团队成员分页")
    @PreAuthorize("@ss.hasPermission('cmt:team-member:query')")
    public CommonResult<PageResult<TeamMemberRespVO>> getTeamMemberPage(@Valid TeamMemberPageReqVO pageReqVO) {
        PageResult<TeamMemberDO> pageResult = teamMemberService.getTeamMemberPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TeamMemberRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出团队成员 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:team-member:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTeamMemberExcel(@Valid TeamMemberPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TeamMemberDO> list = teamMemberService.getTeamMemberPage(pageReqVO).getList();
        ExcelUtils.write(response, "团队成员.xls", "数据", TeamMemberRespVO.class,
                        BeanUtils.toBean(list, TeamMemberRespVO.class));
    }

}
